package com.heifeng.cloudshelf.mvp.programme.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.utils.DensityUtils;

import java.util.List;

/**
 * Created by Administrator on 2019/9/15.
 */

public class ProgrammeImageRvAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private int selectPos = 0;

    public int getSelectPos() {
        return selectPos;
    }

    public void setSelectPos(int selectPos) {
        this.selectPos = selectPos;
    }

    public ProgrammeImageRvAdapter(Context context, int layoutResId, @Nullable List<String> data) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, String item) {
        if (selectPos == helper.getPosition()) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.width = DensityUtils.dp2px(mContext, 138);
            layoutParams.height = DensityUtils.dp2px(mContext, 92);
            layoutParams.setMargins(DensityUtils.dp2px(mContext, 10), 0, DensityUtils.dp2px(mContext, 10), 0);
            helper.getView(R.id.iv_image).setLayoutParams(layoutParams);
        } else {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.width = DensityUtils.dp2px(mContext, 98);
            layoutParams.height = DensityUtils.dp2px(mContext, 66);
            layoutParams.setMargins(DensityUtils.dp2px(mContext, 10), DensityUtils.dp2px(mContext, 10),
                    DensityUtils.dp2px(mContext, 10), DensityUtils.dp2px(mContext, 10));
            helper.getView(R.id.iv_image).setLayoutParams(layoutParams);
        }

        RoundedCorners roundedCorners = new RoundedCorners(2);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(mContext).load(item).apply(options).into((ImageView) helper.getView(R.id.iv_image));

    }
}
